<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\7\19 0019
 * Time: 15:10
 */
namespace app\api\controller\v1;

use app\api\model\UserModel;
use think\Controller;
use think\Db;
use think\Request;

class User extends Controller
{
    /**身份登录接口
     * @param Request $request
     * @return \think\response\Json
     */
    public function login(Request $request){
        $str = UserModel::login($request);
        return json($str);
    }

    /**添加分数
     * @param Request $request
     * @return \think\response\Json
     */
    public function add_fraction(Request $request){
        $user = UserModel::add_fraction($request);
        return json($user);
    }

    /**查询用户信息
     * @param Request $request
     * @return \think\response\Json
     */
    public function getUser(Request $request){
        $token = $request->param('token',null);
        if(!$token){
            return json(['code'=>401,'msg'=>'没有登录']);
        }
        if(!$user=UserModel::istoken($token)){
            return json(['code'=>402,'msg'=>'没有授权']);
        }
        $inviter = $request->param('inviter',null);
        if($inviter){
            if(!UserModel::add_frend($user['openid'],$inviter)){
                return json(['code'=>200,'data'=>$user,'msg'=>'添加朋友失败']);
            };
        }
        return json(['code'=>200,'data'=>$user]);
    }
}